<?php

Yii::import('application.models._base.BaseAlbum');

class Album extends BaseAlbum
{
	public static function model($className=__CLASS__) {
		return parent::model($className);
	}
    public function rules() {
        return array(
            array('title', 'required','message'=>'专辑标题必须填写'),
            array('uid', 'required','message'=>'必须登录','on'=>'create'),
            array('title', 'length', 'max'=>40,'tooLong'=>'专辑标题不得大于40个字符'),
            array('title', 'length', 'min'=>2,'tooShort'=>'专辑标题不得小于2个字符'),
            array('content', 'length', 'max'=>255,'tooLong'=>'专题描述不得大于255个字符'),
            array('tags', 'length', 'max'=>255,'tooLong'=>'专题标签不得大于255个字符'),
            //标签验证
            array('tags', 'checkTags'),
            array('tags,content,is_delete ,list_order , album_cover ,create_time, total_share, total_like', 'safe'),
            array('id ,list_order,is_delete, title, album_cover , content, tags, create_time, uid, total_share, total_like', 'safe', 'on'=>'search'),
        );
    }
    public function getAlbumPk($album_id){
        return $this->findByPk($album_id);
    }
    public function checkTags($attribute,$params){
        if($this->tags){
            $arr = explode(' ',$this->tags);
            if(count($arr) >5){
                $this->addError($attribute,'标签个数不得大于5个');
            }else{
                $ok = true;
                $aaa = '';
                foreach($arr as $k=>$v){
                    if(strlen($v) > 12){
                        $ok = false;
                    }
                }
                if($ok == false){
                    $this->addError($attribute,$ok);
                }
            }
        }
    }

    //验证这个收藏是否是这个用户的
    public function checkAlbum($id,$uid){
        $data = $this->findByPk($id);
        if($data->uid == $uid){
            $return = 1;
        }else{
            $return = 0;
        }
        return $return;
    }

    //在分享中获取列表
    public function getShareAlbum($id){
        $db = Yii::app()->db->createCommand();
        $info = $db->select('id,title')
            ->from('{{album}}')
            ->where('uid=:uid and is_delete=:is_delete',array(':uid'=>$id,':is_delete'=>0))
            ->order('list_order desc,create_time desc')
            ->queryAll();
        return $info;
    }
    //获取喜欢的专辑总数
    public function favoriteAlbumCount($uid){
        $db = Yii::app()->db->createCommand();
        $info = $db->select('COUNT(*) AS count')
            ->from('{{favorite_album}} fa')
            ->leftJoin('{{album}} a','a.id=fa.album_id')
            ->where('fa.user_id=:id and a.is_delete=:is_delete',array(':id'=>$uid,':is_delete'=>0))
            ->queryRow();
        return $info['count'];
    }


    //取得专辑列表
    public function getAlbum($id,$first,$total){
        $db = Yii::app()->db->createCommand();
        $info = $db->select('id,title,create_time,uid,total_share,total_like,album_cover')
           ->from('{{album}}')
            ->where('uid=:uid and is_delete=:is_delete',array(':uid'=>$id,':is_delete'=>0))
            ->order('list_order desc,create_time desc')
            ->limit($total,$first)
            ->queryAll();
        return $info;
    }

    public function getFavoriteAlbum($uid, $first, $total){
        $db = Yii::app()->db->createCommand();
        $info = $db->select('a.id,a.title,a.create_time,a.uid,a.total_share,a.total_like,a.album_cover')
            ->from('{{favorite_album}} fa')
            ->leftJoin('{{album}} a','a.id=fa.album_id')
            ->where('fa.user_id=:id and a.is_delete=:is_delete',array(':id'=>$uid,':is_delete'=>0))
            ->order('a.create_time desc')
            ->limit($total,$first)
            ->queryAll();
        return $info;
    }

    public function getBasicOneAlbum($id){
        $db = Yii::app()->db->createCommand();
        $info = $db->select('a.title,a.id,a.album_cover')
            ->from('{{album}} a')
            ->where('a.id=:id and a.is_delete=:is_delete',array(':id'=>$id,':is_delete'=>0))
            ->queryRow();
        return $info;
    }


    //取得专辑内容以及此用户的信息
    public function getOneAlbum($id){
        $db = Yii::app()->db->createCommand();
        $info = $db->select('fa.album_id as fa,a.id,u.total_followers,a.title,a.tags,total_share,total_like,album_cover,u.username,u.domain,u.nick_name,u.id as uid')
            ->from('{{album}} a')
            ->join('{{user}} u','a.uid=u.id')
            ->leftJoin('{{favorite_album}} fa','fa.album_id=a.id')
            ->where('a.id=:id and a.is_delete=:is_delete',array(':id'=>$id,':is_delete'=>0))
            ->queryRow();
        return $info;
    }

    /**
     * 来自专辑  通过shareid
     * @param $share_id
     * @return mixed
     */
    public function getFromAlbum($share_id){
        $db = Yii::app()->db->createCommand();
        $info = $db->select('a.title,a.id,a.album_cover')
            ->from('{{share}} s')
            ->leftJoin('{{album}} a','a.id=s.album_id')
            ->where('s.id=:id and a.is_delete=:is_delete',array(':id'=>$share_id,':is_delete'=>0))
            ->queryRow();
        return $info;

    }
    public function updateAlbumCount($id,$uid){
        //统计专辑总数 写入user表
        $share_count = Share::model()->count("album_id=:album_id and uid=:uid and is_delete=:is_delete",array(':album_id'=>$id,':uid'=>$uid,':is_delete'=>0));
        $img = $this->showNewItem($id,$uid);
        if($img){
            $image = $img['image_path'];
        }else{
            $image = '';
        }
        $ok = Album::model()->updateByPk($id,array('total_share'=>$share_count,'album_cover'=>$image));
        if ($ok) {
            $data['status'] = 1;
        }else{
            $data['status'] = 0;
        }
        return $data;
    }
    public function showNewItem($id,$uid){
        $db = Yii::app()->db->createCommand();
        $info = $db->select('i.image_path')
            ->from('{{share}} s')
            ->join('{{item}} i','s.iid=i.id')
            ->where('s.album_id=:id and s.uid=:uid and i.is_show=:is_show and i.is_delete=:is_delete and s.is_delete=:sis_delete',array(':id'=>$id,':uid'=>$uid,':is_show'=>1,':is_delete'=>0,':sis_delete'=>0))
            ->order('s.create_time desc')
            ->queryRow();
        return $info;
    }


}